'use strict';
/* Controller for updating  an existing entry*/

// Controller for going onto the right page e.g : modifyEntry.html

blogApp.controller('modifyEntry', ['$scope', '$http', '$stateParams',
   function($scope, $http, $stateParams){
       $http.get('entries/'+$stateParams.entryId).success(function(data) {
        $scope.entry = data;
        console.log(data);
       });
   }
]);


blogApp.controller('updateCtrl',['$scope', '$http','$state',
    function($scope, $http,$state) {

        $scope.submit = function() {

            var entry = {};
            entry.id = this.entry.id;
            entry.title = this.entry.title;
            entry.body = this.entry.body;
            console.log("Saving changes");

            var data = angular.toJson(entry);
            $http({
                url: 'entries/'+entry.id+'/updateEntry',
                method: "PUT",
                data: data,
                headers: {'Content-Type': 'application/json'}
            }).success(function() {
                    $state.go('listAdmin', null, {reload:true});
                });
        };


    }
]);